//处理图片编辑 增加文字等
package com.util.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class PhotoEditing {
    public static void main(String[] args) {
        try {
            // 创建生成器实例
            ImageTextEditor editor = new ImageTextEditor("C:/Users/stiker/Desktop/toux.png");

            // 添加文本项
            editor.addTextItem("name", "张三", 300, 400, new Font("Microsoft YaHei", Font.BOLD, 24), Color.BLACK);
            editor.addTextItem("date", "2025年5月29日", 400, 400, new Font("Microsoft YaHei", Font.PLAIN, 18), Color.BLUE);
//            editor.addTextItem("id", "学号: 20250529", 300, 550, new Font("Microsoft YaHei", Font.PLAIN, 16), Color.DARK_GRAY);

            // 加载自定义字体（如果需要）
//            Font customFont = editor.loadCustomFont("D:/ruoyi/uploadPath/template/impact.ttf", Font.PLAIN, 20);
            editor.addTextItem("school", "某某大学", 600, 400, new Font("Microsoft YaHei", Font.BOLD, 24), new Color(128, 0, 0));

            // 处理图片并保存
            editor.process();
            editor.saveAs("C:/Users/stiker/Desktop/output.png", "png");

            System.out.println("录取通知书生成成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class ImageTextEditor {
    private BufferedImage image;
    private String imagePath;
    private Map<String, TextItem> textItems = new HashMap<>();

    public ImageTextEditor(String imagePath) {
        this.imagePath = imagePath;
    }

    public void addTextItem(String id, String text, int x, int y, Font font, Color color) {
        textItems.put(id, new TextItem(text, x, y, font, color));
    }

    public Font loadCustomFont(String fontPath, int style, float size) throws IOException, FontFormatException {
        Font customFont = Font.createFont(Font.TRUETYPE_FONT, new File(fontPath));
        return customFont.deriveFont(style, size);
    }

    public void process() throws IOException {
        // 加载模板图片
        image = ImageIO.read(new File(imagePath));

        // 创建图形对象
        Graphics2D g2d = image.createGraphics();

        // 启用抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

        // 绘制所有文本项
        for (TextItem item : textItems.values()) {
            g2d.setFont(item.getFont());
            g2d.setColor(item.getColor());
            g2d.drawString(item.getText(), item.getX(), item.getY());
        }

        // 释放资源
        g2d.dispose();
    }

    public void saveAs(String outputPath, String format) throws IOException {
        ImageIO.write(image, format, new File(outputPath));
    }

    // 获取可用字体列表
    public static String[] getAvailableFonts() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        return ge.getAvailableFontFamilyNames();
    }

    // 内部类，表示一个文本项
    private static class TextItem {
        private String text;
        private int x;
        private int y;
        private Font font;
        private Color color;

        public TextItem(String text, int x, int y, Font font, Color color) {
            this.text = text;
            this.x = x;
            this.y = y;
            this.font = font;
            this.color = color;
        }

        public String getText() { return text; }
        public int getX() { return x; }
        public int getY() { return y; }
        public Font getFont() { return font; }
        public Color getColor() { return color; }
    }
}
